package com.hbm.usercenter.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hbm.usercenter.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by BS on 2017/6/8.
 */

public class GroupInfoEye extends LinearLayout {

    @Bind(R.id.tv)
    TextView mTv;
    @Bind(R.id.tv_name)
    EditText mTvName;
    @Bind(R.id.iv_eye)
    ImageView mIvEye;
    @Bind(R.id.h_line)
    View mHLine;
    private boolean isPwdShow = false;
    public GroupInfoEye(Context context) {
        super(context);
        initView(context);
    }

    public GroupInfoEye(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initTypedArray(context, attrs);
    }

    public GroupInfoEye(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initTypedArray(context, attrs);
    }

    private void initView(Context context) {
        if (!isInEditMode()) {
            View view = LayoutInflater.from(context).inflate(R.layout.group_info_eye, this, true);
            ButterKnife.bind(this, view);
        }
    }

    private void initTypedArray(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GroupInfoEye);

        if (null != typedArray) {

            String gourp_tv = typedArray.getString(R.styleable.GroupInfoEye_group_eye_tv);
            String group_pwd = typedArray.getString(R.styleable.GroupInfoEye_group_eye_pwd);
            //是否显示线
            boolean isLine = typedArray.getBoolean(R.styleable.GroupInfoEye_group_eye_is_line,
                    true);
            if (!isInEditMode()) {
                if (!TextUtils.isEmpty(gourp_tv)) {
                    mTv.setText(gourp_tv);
                }
                if (!TextUtils.isEmpty(group_pwd)) {
                    mTvName.setText(group_pwd);
                }
                if (mHLine != null) {
                    if (isLine) {
                        mHLine.setVisibility(View.VISIBLE);
                    } else {
                        mHLine.setVisibility(View.INVISIBLE);
                    }
                }
            }
        }
        typedArray.recycle();
    }
    @OnClick(R.id.iv_eye)
    public void onViewClicked() {
        if (!isPwdShow) {
            mIvEye.setBackgroundResource(R.mipmap.icon_eye);
            mTvName.setInputType(InputType.TYPE_CLASS_TEXT);
            isPwdShow = true;
        } else {
            mIvEye.setBackgroundResource(R.mipmap.icon_un_eye);
            mTvName.setInputType(EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
            isPwdShow = false;
        }
    }

    public String getPwd() {
        return mTvName.getText().toString().trim();
    }
}
